<?php

namespace App\Admin\Controllers;

use App\BrowseRecords;
use App\model\User;
use App\Workshop;
use App\Building;
use App\Land;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;


class BrowseRecordsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('浏览记录')
            ->description('浏览记录')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content

    public function show($id, Content $content)
    {
        return $content
            ->header('浏览记录')
            ->description('浏览记录')
            ->body($this->detail($id));
    }*/

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content

    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }*/

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content

    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }*/

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BrowseRecords);

        $grid->id('Id');
        $grid->user_name('用户名称');
        $grid->type('类型')->display(function ($value) {
            if($value ==0){
                return "厂房";
            }else if($value ==1){
                return "土地";
            }else if($value ==2){
                return "写字楼";
            }else{
                return "其他";
            }
        });
        $grid->produce_id('浏览产品')->display(function ($value) {
            $typeid = $this->type;
            if($typeid ==0){
                return Workshop::find($value)->title;
            }else if($typeid ==1){
                return Land::find($value)->title;
            }else if($typeid ==2){
                return Building::find($value)->title;
            }else{
                return "其他";
            }
            return null;
        });
        $grid->created_at('浏览时间');
        //$grid->updated_at('Updated at');
        //筛选功能
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->like('user_name', '用户名');
                $filter->equal('user_id', '用户id');
            });
            $filter->column(1 / 2, function ($filter) {
                $type = [
                    ' ' => '全部',
                    0 => '厂房',
                    1 => '土地',
                    2 => '写字楼',
                ];
                $filter->equal('type', '类型')->select($type);
                $produce_type = \Request::get('type');
                if (isset($produce_type)) {
                    if($produce_type ==0){
                        $filter->equal('produce_id', '产品名称')->select(Workshop::getSelectOptions());
                    }else if($produce_type ==1){
                        $filter->equal('produce_id', '产品名称')->select(Land::getSelectOptions());
                    }else if($produce_type ==2){
                        $filter->equal('produce_id', '产品名称')->select(Building::getSelectOptions());
                    }
                }
            });
        });
        $grid->disableCreateButton();
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show

    protected function detail($id)
    {
        $show = new Show(BrowseRecords::findOrFail($id));

        $show->id('Id');
        $show->user_id('User id');
        $show->type('Type');
        $show->produce_id('Produce id');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }*/

    /**
     * Make a form builder.
     *
     * @return Form

    protected function form()
    {
        $form = new Form(new BrowseRecords);

        $form->number('user_id', 'User id');
        $form->text('type', 'Type');
        $form->text('produce_id', 'Produce id');

        return $form;
    }*/
    public function  type(Request $request ){
        $provinceId = $request->get('q');
        if($provinceId ==0){
            return Workshop::get(['id', \DB::raw('title as text')]);
        }else if($provinceId ==1){
            return Land::get(['id', \DB::raw('title as text')]);
        }else if($provinceId ==2){
            return Building::get(['id', \DB::raw('title as text')]);
        }

    }
}
